<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <button @click="fn">销毁</button>
        <h1>{{msg}}</h1>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        new Vue({
            data() {
                return {
                    msg: 'hello'
                }
            }
        }).$mount('#app')
    </script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h1>{{msg}}</h1>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        let timerId = null
        new Vue({
            // 阶段一:挂载
            beforeCreate() {
                console.log('1 beforeCreate');
                console.log(this.msg);//undefined 数据没有准备好
                console.log(this.fn);//undefined 方法也没有准备好 
                console.log(this.$el);//undefined 视图也没有准备好
                //清理当前组件中的定时器
                clearInterval(timerId)
            },
            //在这个阶段我们可以操作数据和方法 我们一般在后面写项目的时候就是在这个钩子里面发送axios请求
            created() {
                console.log('2 created');
                console.log(this.msg);//hello 数据准备好
                console.log(this.fn);//方法准备好 
                console.log(this.$el);//undefined 视图也没有准备好
            },
            beforeMount() {
                console.log('3   beforeMount')
                console.log(this.msg);//hello 数据准备好
                console.log(this.fn);//方法准备好 
                console.log(this.$el);//undefined 视图也没有准备好
            },
            mounted() {
                console.log('4   mounted')
                console.log(this.msg);//hello 数据准备好
                console.log(this.fn);//方法准备好 
                console.log(this.$el);//数据和视图同步
                timerId = setInterval(() => {
                    console.log('定时器触发了');
                }, 500)
            },
            data() {
                return {
                    msg: 'hello'
                }
            },
            methods: {
                fn() {
                    this.$destroy()
                }
            }
        }).$mount('#app')
    </script>
</body>

</html>